# !/usr/bin/env python
# coding=utf-8
# @Time    : 2017/12/27 17:06
# @Author  : 6imensiva
# @Site    : https://gitee.com/dimensiva/wx_server_prototype.git
# @File    : signature.py
# @Software: PyCharm
# @Version: 3.5.4
from flask import request
from configs import current_config
from . import api
from utils import my_string, my_time, md5
from app.service import token_service, ticket_service
from app.common import my_response


@api.route(current_config.API_PREFIX + '/signature', methods=['GET'])
def get_signature():
	url = request.args.get('url')
	noncestr = my_string.get_random_string(12)
	timestamp = my_time.get_timestamp()

	token = token_service.get_server_access_token2(False)
	jsapi_ticket = ticket_service.get_jsapi_ticket(token)
	src_signature = 'jsapi_ticket=' + jsapi_ticket + '&noncestr=' + noncestr + \
	                '&timestamp=' + str(timestamp) + '&url=' + url

	dst_signature = md5.encode_with_sha1(src_signature)
	return my_response.success({'noncestr': noncestr,
	                            'timestamp': timestamp,
	                            'signature': dst_signature,
	                            'appId': current_config.APPID})